//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Graphics/MetaData/Meta.RenderState.h"

#ifdef ENGINE_META_DATA
#	include "Graphics/Resources/Sprite2D.h"
#	include "Graphics/Systems/BatchRender.h"
#endif


#	ifdef ENGINE_META_DATA
	namespace MetaData
	{

		template <>
		inline void MetaDataUtils::Copy<> (ProgramPtr &left, string &right, const EngineSubSystemsRef ess)
		{
			if ( right.Empty() )
				return;

			CHECK_ERR( ess->GetResourceManager()->Load( EResource::PROGRAM, right, left, Program::GetDefaultLoadParams() ), void() );
		}
		
		template <>
		inline void MetaDataUtils::Copy<> (Sprite2DPtr &left, string &right, const EngineSubSystemsRef ess)
		{
			if ( right.Empty() )
				return;

			CHECK_ERR( ess->GetResourceManager()->Load( EResource::SPRITE_2D, right, left, Sprite2D::GetDefaultLoadParams() ), void() );
		}

	}	// MetaData
#	endif


	
	//
	// Batch Material
	//
#	define META_OBJECT			BatchMaterialCfg
#	define META_OBJECT_NAME		TOSTRING( BatchMaterial )
#	define META_NAME			MetaBatchMaterial

#	define META_FIELDS( _decl ) \
		_decl( ColorStateSeparateCfg,	colorState,			,	) \
		_decl( string,					program,			,	) \
		_decl( bool,					colorStateDefined,	,	false )
			
#	include "Base/MetaData/MetaDataObject.h"
	

#	ifdef ENGINE_META_DATA
	namespace MetaData
	{
		template <>
		inline void MetaDataUtils::Copy<> (BatchMaterial &left, BatchMaterialCfg &right, const EngineSubSystemsRef ess)
		{
			ProgramPtr	prog;
			MetaDataUtils::Copy( prog, right.program, ess );
			left.SetProgram( prog );

			if ( right.colorStateDefined )
			{
				RenderState::ColorStateSeparate	cs;
				MetaDataUtils::Copy( cs, right.colorState, ess );
				left.SetColorState( cs );
			}
		}

	}	// MetaData
#	endif



	//
	// Sprite 2D Animation
	//
#	define META_OBJECT			Sprite2dAnimationCfg
#	define META_OBJECT_NAME		TOSTRING( Sprite2DAnim )
#	define META_BASECLASS		Sprite2D::Animation

#	define META_FIELDS( _decl ) \
		_decl( uvec2,		frameInterval,		frameInterval,	) \
		_decl( float,		rate,				rate,			) \
		_decl( bool,		blend,				blend,			)
		//_decl( uvec2,		type,				type,			) \
			
#	include "Base/MetaData/MetaDataObject.h"



	//
	// Sprite 2D Physics
	//
#	define META_OBJECT			Sprite2dPhysicsCfg
#	define META_OBJECT_NAME		TOSTRING( Sprite2DPhysics )
#	define META_NAME			MetaSprite2DPhysics
#	define META_BASECLASS		Sprite2D::Physics

#	define META_FIELDS( _decl ) \
		_decl( int,			shape,				shape,			) \
		_decl( vec2,		scale,				scale,			) \
		_decl( vec2,		center,				center,			) \
		_decl( float,		friction,			friction,		) \
		_decl( float,		mass,				mass,			)
			
#	include "Base/MetaData/MetaDataObject.h"



	//
	// Sprite 2D
	//
#	define META_OBJECT			Sprite2dCfg
#	define META_OBJECT_NAME		TOSTRING( Sprite2D )
#	define META_BASECLASS		Sprite2D

	namespace MetaData
	{
		typedef map< uvec2, Sprite2dAnimationCfg >	Sprite2dAnimationMapCfg;
		
#	ifdef ENGINE_META_DATA
		
		template <>
		inline void MetaDataUtils::Copy<> (Sprite2D::anim_map_t &left, Sprite2dAnimationMapCfg &right, const EngineSubSystemsRef ess)
		{
			left.Reserve( right.Count() );

			FOR( i, right )
			{
				Sprite2D::Animation	anim;
				MetaDataUtils::Copy( anim, right[i].second, ess );

				left.Add( right[i].first, anim );
			}
		}

#	endif
	}

#	define META_FIELDS( _decl ) \
		_decl( Sprite2dAnimationMapCfg,		animations,		_animations,	) \
		_decl( array< rect >,				texcoords,		_texcoords,		) \
		_decl( uint32_array_t,				frames,			_frames,		) \
		_decl( BatchMaterialCfg,			material,		_material,		) \
		_decl( color4u,						color,			_color,			color4u(0xFF) )
		//_decl( Sprite2dPhysicsCfg,		physics,		_physics,		)
			
#	include "Base/MetaData/MetaDataObject.h"

